//
//  DownloadSession.m
//  ZXDownload
//
//  Created by xin on 2018/5/14.
//  Copyright © 2018年 魏鑫. All rights reserved.
//

#import "DownloadSession.h"

@interface DownloadSession () <NSURLSessionDataDelegate>

@property (strong, nonatomic) NSURLSession *session;

@end

@implementation DownloadSession

- (instancetype) init
{
    self = [super init];
    if (self) {
        [self _config];
    }
    return self;
}

- (void)_config
{
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
    self.session = session;
}

- (void)startRequestWithUrl:(NSString *)url
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request];
    task.taskDescription = url;
    [task resume];
}

- (void)stop
{
    
}

#pragma mark - <NSURLSessionDataDelegate>
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSHTTPURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
    NSLog(@"didReceiveResponse=%lld",dataTask.countOfBytesExpectedToReceive);
    completionHandler(NSURLSessionResponseAllow);
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    NSLog(@"didReceiveData=%lu",(unsigned long)data.length);
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    NSLog(@"didCompleteWithError=%@",error);
}

@end
